<?php

namespace app\common\bootstrap;

use \Illuminate\Support\Str;
use support\facade\Logger;
use Webman\Bootstrap;
use support\Db;

class LaravelLog implements Bootstrap
{
    public static function start($worker){
        Db::listen(function($query){
            $sql = trim($query->sql);
            if(strpos($sql,'global_log')){
               // 全局的日志记录表  不需要再记录了
               return false;
           }
            // 心跳 不用记录
            if (strtolower($sql) === 'select 1') {
                return false;
            }
            $bindings = [];
            if ($query->bindings) {
                foreach ($query->bindings as $v) {
                    if (is_numeric($v)) {
                        $bindings[] = $v;
                    } else {
                        $bindings[] = '"' . strval($v) . '"';
                    }
                }
            }
            $execute = Str::replaceArray('?', $bindings, $sql);
            Logger::sql($execute);
        });
    }
}